package com.udf.hive.udf.extract;

import java.util.logging.Logger;

import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;

/**
 * 数据提取-单分隔符
 * 
 * @作者 tao zhou
 * @version [版本号, 2020年7月18日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class SingleExtractUDF extends UDF
{
    private static Logger logger = Logger.getLogger("FirstCharUpperCaseUDF");
    
    /**
     * 数据处理逻辑
     * 
     * @param inputField
     *            输入字段
     * @param splitType
     *            取值方式 0: 前 ,1:后
     * @param splitStr
     *            分隔符
     * @param splitStrIndex
     *            分隔符位点
     * @param isRetainSplitStr
     *            是否保留分隔符 0: 否 ,1:是
     * @return
     * @exception/throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public Text evaluate(Text inputField, Text splitType, Text splitStr, Text splitStrIndex, Text isRetainSplitStr) {
        if (inputField == null) {
            return null;
        }
        // 参数1： 字段，参数2：取值方式，参数3：分隔符，参数4：分隔符位点，参数5：是否保留分隔符
        String inputFieldStr = String.valueOf(inputField).trim();
        String splitTypeStr = String.valueOf(splitType).trim();
        String regex = String.valueOf(splitStr).trim();
        String splitStrIndexStr = String.valueOf(splitStrIndex).trim();
        String isRetainSplit = String.valueOf(isRetainSplitStr).trim();
        Integer splitTypeInt = Integer.parseInt(splitTypeStr);
        Integer splitStrIndexInt = Integer.parseInt(splitStrIndexStr);
        Integer isRetainSplitInt = Integer.parseInt(isRetainSplit);
        String result = null;
        // 目前只对整型做转换
        try {
            result = singleExtract(inputFieldStr, splitTypeInt, regex, splitStrIndexInt, isRetainSplitInt);
        }
        catch (Exception e) {
            e.printStackTrace();
            result = inputFieldStr;
        }

        return new Text(result);
    }

    public String singleExtract(String inputFieldStr, Integer splitTypeInt, String regex, Integer splitStrIndexInt,
            Integer isRetainSplitInt) {
        String result;
        int fromIndex = 0;
        int regexIndex = 0;
        for (int i = 1; i <= splitStrIndexInt; i++) {
            regexIndex = inputFieldStr.indexOf(regex, (1 == i ? fromIndex : fromIndex + regex.length()));
            fromIndex = regexIndex;
        }
        if (0 == splitTypeInt) {
            result = inputFieldStr.substring(0, regexIndex) + (1 == isRetainSplitInt ? regex : "");
        }
        else {
            result = inputFieldStr.substring(1 == isRetainSplitInt ? regexIndex + regex.length() : regexIndex);
        }
        return result;
    }

    public static void main(String[] args) {
        logger.info(String.valueOf(new SingleExtractUDF().evaluate(new Text("江苏省苏州市"), new Text("0"), new Text("省"),
                new Text("1"), new Text("1"))));
        logger.info(String.valueOf(new SingleExtractUDF().evaluate(new Text("江苏省苏州市"), new Text("0"), new Text("省"),
                new Text("1"), new Text("0"))));
        logger.info(String.valueOf(new SingleExtractUDF().evaluate(new Text("江苏省苏州市"), new Text("1"), new Text("省"),
                new Text("1"), new Text("1"))));
        logger.info(String.valueOf(new SingleExtractUDF().evaluate(new Text("江苏省苏州市"), new Text("1"), new Text("省"),
                new Text("1"), new Text("0"))));
    }
}
